/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.html;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.List;

import twaver.AlarmSeverity;
import twaver.BTS;
import twaver.BTSAntenna;
import twaver.Element;
import twaver.Generator;
import twaver.Link;
import twaver.Node;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.network.TNetwork;
import twaver.network.background.TextureBackground;
import twaver.network.ui.ElementUI;
import demo.DemoPane;

public class HTMLDemo extends DemoPane {
	
    TDataBox box = new TDataBox();
    TNetwork network = new TNetwork(box);

    public HTMLDemo() {
    	this.network.setToolbarByName(TWaverConst.DEFAULT_TOOLBAR);
    	this.network.clearMovableFilters();
        this.add(network, BorderLayout.CENTER);
        
        BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.setColor(new Color(84, 107, 250));
        g2d.fillRect(0, 0, 200, 200);
        Font font = new Font("Dialog", Font.ITALIC, 16);
        AffineTransform at = new AffineTransform();
        at.rotate(-Math.PI/6);
        font = font.deriveFont(at);
        g2d.setFont(font);
        g2d.setColor(Color.ORANGE);
        g2d.drawString("www.servasoftware.com", 50, 150);
        g2d.setColor(Color.ORANGE.darker());
        g2d.drawString("www.servasoftware.com", 51, 151);
        g2d.dispose();

        // Create a texture paint from the buffered image
        Rectangle rect = new Rectangle(0, 0, 200, 200);
        TexturePaint texture = new TexturePaint(bufferedImage, rect);
        network.setBackground(new TextureBackground(texture));
        network.setEnsureVisibleOnSelected(true);
        network.setEnableAttachmentDefaultAction(true);
        
        final Node node = new Node();
        node.setLocation(180, 100);
        node.setImage("/demo/resource/images/pc.png");
        box.addElement(node);
        node.getAlarmState().addNewAlarm(AlarmSeverity.WARNING);
        node.putAlarmBalloonOutlineColor(Color.WHITE);
        node.putBorderVisible(false);
        node.setName(
        		"<html>In case you thought that TWaver label had to be" +
			    "<p>boring, one line descriptions, the " +
			    "<p><font color=yellow size=+2>TWaver Team</font>" +
			    "<p> is happy to shatter your illusions.<p>" +
			    "In TWaver, they can use HTML to " +
			    "<ul><li>Have Lists<li><b>Bold</b> " +
			    "text<li><em>emphasized</em> " +
			    "text<li>text with <font color=red>Color</font>" +
			    "<li>text in different <font size=+3>sizes</font>" +
			    "<li>and <font face=AvantGarde>Fonts</font></ul>" +
			    "Oh, and they can be multi-line, too.</html>");
        box.getSelectionModel().setSelection(node);
        
        final BTS bts = new BTS();
        bts.setLocation(530, 380);
        bts.addAttachment(TWaverConst.ATTACHMENT_MESSAGE);
        bts.putMessagePosition(TWaverConst.POSITION_CENTER);
        bts.putMessageBackground(new Color(0, 0, 255, 128));
        bts.putMessageGradient(true);
        bts.putMessageGradientColor(new Color(0, 0, 0, 160));
        bts.putMessageForeground(Color.GREEN);
        bts.putMessageShadowVisible(false);
        bts.putMessageBorderVisible(true);
        bts.putMessageBorderColor(Color.WHITE);
        bts.putMessageBorderStroke(TWaverConst.STROKE_SOLID_MIDDLE);
        bts.putMessageMinimizable(true);
        
        final BTSAntenna antennaA = new BTSAntenna();
        bts.addAntenna(antennaA);
        
        final BTSAntenna antennaB = new BTSAntenna();
        antennaB.setBeamDirection(270);
        antennaB.setBeamWidth(30);
        antennaB.setPower(150);
        antennaB.putBodyColor(Color.RED);
        bts.addAntenna(antennaB);

        final BTSAntenna antennaC = new BTSAntenna();
        antennaC.setBeamDirection(270);
        antennaC.setBeamWidth(90);
        antennaC.setPower(50);
        antennaC.putTextureFactory(TWaverConst.TEXTURE_GRID_DIAMOND);
        antennaC.putBodyColor(Color.BLUE);
        bts.addAntenna(antennaC);
        
        box.addElement(bts);
        box.addElement(antennaA);
        box.addElement(antennaB);
        box.addElement(antennaC);
        
        network.setParallelLinkGap(15);
		for (int i = 1; i < 6; i++) {
			Link link = new Link();
			link.setLinkType(TWaverConst.LINK_TYPE_XSPLIT);
			link.putAlarmBalloonTextFont(TUIManager.getDefaultItalicBoldFont());
			link.putLinkWidth(2);
			link.putLinkAntialias(true);
			link.putAlarmBalloonAlpha(0.7f);
			link.putClientProperty("linkname", "link" + i);
			link.putLinkBundleExpand(false);
			link.setFrom(node);
			link.setTo(bts);
			link.getAlarmState().setNewAlarmCount(AlarmSeverity.getByValue(i * 100), i);
			box.addElement(link);
		}
             
        TaskScheduler.getInstance().register(new TaskAdapter(){
            public void run(long clock) {
            	node.getAlarmState().addNewAlarm(AlarmSeverity.WARNING);
            	node.putClientProperty("alarmTime", new Date());
            	
            	antennaB.getAlarmState().clear();
            	antennaB.getAlarmState().addNewAlarm(TWaverUtil.getRandomNonClearedSeverity());     
            	int direction = (antennaB.getBeamDirection() + 30) % 360; 
            	antennaB.setBeamDirection(direction);         
    			if(direction > 0 && direction < 90){
    				bts.putMessageDirection(TWaverConst.ATTACHMENT_DIRECTION_BOTTOM_LEFT);
    			}
    			else if(direction >= 90 && direction < 180){
    				bts.putMessageDirection(TWaverConst.ATTACHMENT_DIRECTION_BOTTOM_RIGHT);
    			}
    			else if(direction >= 180 && direction < 270){
    				bts.putMessageDirection(TWaverConst.ATTACHMENT_DIRECTION_TOP_RIGHT);
    			}
    			else{
    				bts.putMessageDirection(TWaverConst.ATTACHMENT_DIRECTION_TOP_LEFT);
    			}
    			
    			antennaA.setPower(10 + 180 - direction/2);
    			antennaA.putBodyColor(new Color(255, (int)(255-direction/360.0*255), 0));
    			
            	bts.putMessageContent(
            			"<html>" + 
            			"A Beam direction: " + antennaA.getBeamDirection() + "<br>" + 
            			"B Beam direction: <font color=red>" + antennaB.getBeamDirection() + "</font><br>" + 
            			"C Beam direction: " + antennaC.getBeamDirection() + "<br>" + 
            			"</html>");
            }
        });
     
        network.setAlarmLabelGenerator(new Generator(){
			public Object generate(Object ui) {
				ElementUI elementUI = (ElementUI)ui;
				Element element = elementUI.getElement();
		        AlarmSeverity severity = element.getAlarmState().getHighestNewAlarmSeverity();
		        if (element instanceof Link) {
					Link link = (Link) element;
					List links = box.getBundledLinks(link);
					int size = links.size();
					if (size > 1) {
						if (!link.isLinkBundleExpand()) {
							StringBuffer text = new StringBuffer();
							text.append("<html>");
							for (int i = 0; i < size; i++) {
								Link l = (Link) links.get(i);
								AlarmSeverity highSeverity = l.getAlarmState().getHighestNewAlarmSeverity();
								if (highSeverity != null) {
									int highestSeverityCount = l.getAlarmState().getNewAlarmCount(highSeverity);
									String bubbleString = highestSeverityCount + highSeverity.getNickName();
									if (l.getAlarmState().hasLessSevereNewAlarms()) {
										bubbleString += "+";
									}
									text.append("<tr>");

									String color = TWaverUtil.getColorHexString(highSeverity.getColor().brighter(), true);
									text.append("<td>");
									text.append("<font color=" + color + ">" + l.getClientProperty("linkname") + ":&nbsp;</font>");
									text.append("</td>");

									text.append("<td>");
									text.append("<font color=" + color + ">" + bubbleString + "</font>");
									text.append("</td>");

									text.append(" </tr>");
								}
							}
							text.append("</html>");
							return text.toString();
						}
					}
				}
		        
		        if (severity != null){
		            int highestSeverityCount = element.getAlarmState().getNewAlarmCount(severity);
		            Date time = (Date)element.getClientProperty("alarmTime");
		            if(time == null){
			            String bubbleString = highestSeverityCount + severity.getNickName();
			            if(element.getAlarmState().hasLessSevereNewAlarms()){
			            	bubbleString += "+";
			            }
			            return bubbleString;
		            }else{
		            	return "<html>Arises <font color=red>" + highestSeverityCount + "</font> counts " 
		            	+ "<font color=blue>" + severity.getDisplayName() + "</font> at:<br>" + time + "</html>";
		            }
		        }
				
				return null;
			}
        });
    }
}